var app = getApp()
//获取电影类型数组
var doubanMovieType = app.globalData.g_doubanMovieType
//处理星星组件的方法
function resolveRating(rating){
    //根据0.5取整
    var ratingNum = Math.floor(Math.round(rating)/2*2)/2
    var hasHalf = ratingNum%1==0?false:true
    var ratingFlag = parseInt(ratingNum)
    var ratingArr = []
    for(var i=1;i<=ratingFlag;i++){
      ratingArr.push('full')
    }
    if(hasHalf){
      ratingArr.push('half')
    }
    while(ratingArr.length<5){
      ratingArr.push('empty')
    }
    return {
        'rating':rating,
        'ratingArr':ratingArr
    }
}
//处理字符串连接的函数,用在演员名字连接的时候
function concatName(arr,arrAttr){
    var str = ''
    if(arr.length>0){
      for(var i=0;i<arr.length;i++){
        str += arr[i][arrAttr] + ' / '
      }
      str = str.substring(0,str.length-2)
    }
    return str
  }

//删除演员重复数据的函数
function deleteRepeat(arr) {
  var new_arr = []
  var repeat = false
  for (var i = 0; i < arr.length; i++) {
    repeat = false
    for (var j = 0; j < new_arr.length; j++) {
      if (arr[i].id == new_arr[j].id) {
        repeat = true
        break
      }
    }
    if (!repeat) {
      new_arr.push(arr[i])
    }
  }
  return new_arr
}


//将电影类型转换成标题的方法
exports.convertMovieType = function(movieType,arrType){
    arrType = arrType || 'title'
    for(var key in doubanMovieType){
      if(doubanMovieType[key].name == movieType){
        return doubanMovieType[key][arrType]
      }
    }
}
//获取豆瓣信息
exports.getDoubanMsg = function(url,callback){
    wx.request({
          url: url,
          method: 'GET',
          header: {
              'Content-Type':'json'
          }, // 设置请求的 header
          success: function(res){
            //将对数据的处理交给回调函数
            callback(res.data)
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
}
//处理豆瓣信息
exports.resolveDoubanMsg = function(data){
    var movies = []
    var dataSubjects = data.subjects
    for(var i=0;i<dataSubjects.length;i++){
        var movieId = dataSubjects[i].id
        var title = dataSubjects[i].title
        var rating = dataSubjects[i].rating.average
        var image = dataSubjects[i].images.large
        movies.push({
          "movieId":movieId, 
          "title":title,
          "rating":resolveRating(rating),
          "image":image
        })
    }
    return movies 
}
//处理豆瓣电影详情信息
exports.resolveDoubanMovie = function (data) {
  //电影海报
  var post = data.images ? data.images.large : ''
  //电影标题
  var title = data.title
  //所属地区
  var country = data.countries.join(' ')
  //上映年份
  var year = data.year
  //喜欢的人数
  var loveNum = data.wish_count
  //评论人数
  var commentNum = data.comments_count
  //评分情况
  var rating = resolveRating(data.rating ? data.rating.average : 0)
  //影人
  // {
  //   name:'',
  //   avatar:''
  // }
  var products = []
  var casts = data.directors.concat(data.casts)
  if (casts.length > 0) {
    for (var i = 0; i < casts.length; i++) {
      products.push({
        'name': casts[i].name,
        'avatar': casts[i].avatars ? casts[i].avatars.large : '',
        'id': casts[i].id

      })
    }
  }
  products = deleteRepeat(products).slice(0, 5)
  //导演
  var director = concatName(data.directors, 'name')
  //演员
  var actors = concatName(data.casts, 'name')
  //类型
  var movieType = data.genres.join('、')
  //剧情简介
  var summary = data.summary

  return {
    post: post,
    title: title,
    country: country,
    year: year,
    loveNum: loveNum,
    commentNum: commentNum,
    rating: rating,
    products: products,
    director: director,
    actors: actors,
    movieType: movieType,
    summary: summary

  }
}